package com.erpanderp.wam.ac.model.service.travel;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.project.ProjectAssignmentDetail;
import com.erpanderp.wam.ac.model.entity.travel.Logistics;
import com.erpanderp.wam.ac.model.entity.travel.TravelRequest;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class TravelRequestDAO {
	public String getLastGeneratedTravelRequestCode() {
		String queryString = "select t.travelRequestCode from TravelRequest t";

		List<String> codeList = null;

		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			codeList = (List<String>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
		}

		String lastCode = null;
		for (String code : codeList) {
			if(code != null && code.startsWith("E"))
				continue;
			if (lastCode == null) {
				lastCode = code;
				continue;

			} else if (Long
					.parseLong(code.substring(code.lastIndexOf("-") + 1)) > Long
					.parseLong(lastCode.substring(lastCode.lastIndexOf("-") + 1))) {
				lastCode = code;
				continue;
			}
			System.out.println(lastCode + "============ " + code);
		}
		return lastCode;
	}

	public TravelRequest createTravelRequest(TravelRequest travelRequest) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.save(travelRequest);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return travelRequest;
	}
	
	public List<TravelRequest> getTravelRequests(Long employeeId) {
		List<TravelRequest> travelRequests = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from TravelRequest tr where tr.employee.employeeId = :employeeId");
			System.out.println(query == null);
			query.setCacheable(false).setLong("employeeId",	employeeId);
			travelRequests = (List<TravelRequest>) query.list();
			session.getTransaction().commit();
			return travelRequests;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<TravelRequest> getTravelRequestsByApproverId(Long employeeId, String status) {
		List<TravelRequest> travelRequests = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			
			String queryString = "from TravelRequest tr where tr.approver.employeeId = :employeeId and tr.employee.status =:employeeStatus";
			if(status != null && !status.equals(""))
				queryString += " and tr.travelStatus = :status";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("employeeId",	employeeId).setString("employeeStatus", CommonLookup.STATUS_ACTIVE);
			if(status != null && !status.equals(""))
				query.setString("status", status);
			
			travelRequests = (List<TravelRequest>) query.list();
			session.getTransaction().commit();
			return travelRequests;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<TravelRequest> getAllTravelRequests() {
		List<TravelRequest> travelRequests = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from TravelRequest tr");
			System.out.println(query == null);
			query.setCacheable(false);
			travelRequests = (List<TravelRequest>) query.list();
			session.getTransaction().commit();
			return travelRequests;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public TravelRequest getTravelRequest(String travelRequestCode) {
		TravelRequest travelRequest = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from TravelRequest tr where tr.travelRequestCode = :travelRequestCode");
			System.out.println(query == null);
			query.setCacheable(false).setString("travelRequestCode",	travelRequestCode);
			List temp = (List<TravelRequest>) query.list();
			session.getTransaction().commit();
			if(temp != null && temp.size() > 0){
				travelRequest = (TravelRequest) temp.get(0);
			}
			return travelRequest;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public TravelRequest getTravelRequestById(Long travelRequestId) {
		TravelRequest travelRequest = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			travelRequest = (TravelRequest) session.load(TravelRequest.class, travelRequestId);
			session.getTransaction().commit();
			return travelRequest;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public TravelRequest updateTravelRequest(TravelRequest travelRequest) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(travelRequest);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return travelRequest;
	}
	
	public static void main(String q[]){
		//System.out.println(new TravelRequestDAO().getTravelRequests(2l));
		System.out.println(new TravelRequestDAO().getTravelRequestsByApproverId(2l, CommonLookup.TRAVEL_REQUEST_STATUS_APPROVED));
	}
}
